<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/20 0020
 * Time: 下午 20:58
 */

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\Menulist;
use think\Controller;
use think\Cookie;

class Base extends Controller
{
    public function menuLoad(){
        $admin = new Admin();
        $adminWhere['id'] = Cookie::get('admin_id');
        $adminInfo = $admin->getOne($adminWhere);
        $menu = new Menulist();
        if($adminInfo['show_menu_id'] != 'all'){
            $topWhere['id'] = array('in', $adminInfo['show_menu_id']);
        }else{
            $topWhere['pid'] = 0;
        }
        $topMenu = $menu->getAllMenu($topWhere);
        foreach($topMenu as $j=>$k){
            $secondWhere['pid'] = $k['id'];
            $secondMenu = $menu->getAllMenu($secondWhere);
            $k['secondMenu']= $secondMenu;
        }

        return $this->assign('topMenu',$topMenu);
    }

    public function checkLogin(){
        if(!Cookie::has('admin_id')){
            return $this->error('未登陆','admin/login/');
        }
    }

    /**
     * @param $fileName
     * @return bool
     * 检查文件是否存在
     */
    public function checkMenuController($fileName){

        $file = '../application/admin/controller';
        //1、首先先读取文件夹
        $temp = scandir($file);
        unset($temp[0]);
        unset($temp[1]);
        if(in_array($fileName.".php", $temp)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param $fileName model 文件名
     * @return bool
     */
    public function checkModelFile($fileName){
        $file = '../application/admin/model';
        //1、首先先读取文件夹
        $temp = scandir($file);
        unset($temp[0]);
        unset($temp[1]);
        if(in_array($fileName.".php", $temp)){
            return true;
        }else{
            return false;
        }
    }

    public function ajaxResult($status){
        if($status == 1){
            $result['code'] = 1;
            $result['msg'] = "执行成功";
            echo json_encode($result);
        }else if($status == 0){
            $result['code'] = 0;
            $result['msg'] = "执行失败";
            echo json_encode($result);
        }else{
            $result['code'] = 3;
            $result['msg'] = "权限不足";
            echo json_encode($result);
        }
    }
}